import Vue from "vue";
import Router from "vue-router";

import Index from "./components/Index.vue";
import Home from "./components/Home.vue";
import Welcome from "./components/Welcome.vue";

// import
Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    {
      path: "/",
      // redirect: '/index',
      component: Index,
    },
    {
      path: "/index",
      name: "index",
      component: Index,
    },
    {
      path: "/accountSetting",
      name: "accountSetting",
      component: () => import("./components/account/AccountSetting.vue"),
    },
    {
      path: "/resetPassword",
      name: "resetPassword",
      redirect: "/",
    },
    {
      path: "/home",
      name: "home",
      component: Home,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          name: "welcome",
          component: Welcome,
        },
        {
          // 查看培训记录
          path: "/reviewRecords",
          name: "reviewRecords",
          component: () => import("./views/records/reviewRecords.vue"),
        },
        {
          // 上传培训记录
          path: "/uploadRecord",
          name: "uploadRecord",
          component: () => import("./views/records/uploadRecord.vue"),
        },
        {
          // 培训记录整理统计页面
          path: "/statisticsRecord",
          name: "statisticsRecord",
          component: () => import("./views/records/statisticsRecord.vue"),
        },
        {
          // 查看荣誉奖项
          path: "/reviewPrizes",
          name: "reviewPrizes",
          component: () => import("./views/prizes/reviewPrizes.vue"),
        },
        {
          // 上传荣誉奖项
          path: "/uploadPrize",
          name: "uploadPrize",
          component: () => import("./views/prizes/uploadPrize.vue"),
        },
        {
          // 组织架构
          path: "/structure",
          name: "structure",
          component: () => import("./views/structure/structure.vue"),
        },
        {
          // 单个培训记录页面
          path: "/record/:recordId",
          name: "record",
          component: () => import("./views/records/singleRecord.vue"),
        },
        {
          // 本组管理
          path: "/group",
          name: "groupManagement",
          component: () => import("./views/structure/groupManagement.vue"),
        },
        {
          // 配置管理
          path: "/config",
          name: "itConfig",
          component: () => import("./views/itconfig/itconfig.vue"),
        },
      ],
    },
  ],
});

/**
 * 解决报错问题：报错显示是路由重复
 * Error: Avoided redundant navigation to current location
 */
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
/**
 *  路由守卫，登录拦截
 */

import { Notification } from "element-ui";

router.beforeEach((to, from, next) => {
  let guest = window.sessionStorage.getItem("isGuest") == "1";
  let notGuest = ["/uploadRecord", "/uploadPrize", "/group"];
  // to将要访问的路径
  // from 代表从那个路径跳转而来
  // next 一个函数表示放行
  // next("/login") 强制跳转
  // console.log("to", to);
  // console.log("able", ableToGo);
  if (guest && notGuest.includes(to.path)) {
    Notification({
      title: "别乱点噢~",
      message: "IT培优的小伙伴才能进入这里噢！",
      type: "warning",
      duration: 3000,
      offset: 66,
    });
    return;
  }
  // const token = window.sessionStorage.getItem("token");
  // if (!token) return next("/login"); //如果是未登录装套转到登录页面
  next();
});

export default router;
